<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/AdminIndex' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>系统信息</el-breadcrumb-item>
    <el-breadcrumb-item>用户列表</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 搜索导航栏 -->
  <div class="con" >
    <div style="float: left">
      <el-button type="primary" icon="el-icon-edit"  @click="addDialogVisible = true">新建</el-button>
      <el-button type="success" icon="el-icon-share" @click="downList">导出</el-button>
    </div>

    <div style="float: right">
      <!-- 搜索添加 -->
      <el-input placeholder="请输入搜索的账户" clearable v-model="queryInfo.query"  @clear="getUserList">
        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
      </el-input>
    </div>
  </div>


  <!-- 信息展示栏 -->
  <div class="con">
    <el-table :data="userList" border stripe>
      <el-table-column label="序号"  type="index" width="50px"></el-table-column>
      <el-table-column label="账户" prop="account"></el-table-column>
      <el-table-column label="用户名称" prop="user_name"></el-table-column>
      <el-table-column label="用户类别" width="200" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.user_kind_id==0" >admin</el-tag>
          <el-tag v-if="scope.row.user_kind_id==2" type="warning">商家</el-tag>
          <el-tag v-if="scope.row.user_kind_id==1" type="danger">用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" prop="phone"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button v-if="scope.row.user_kind_id!=0" type="primary" icon="el-icon-edit" size="medium" @click="showEditDialog(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button v-if="scope.row.user_kind_id!=0" type="danger" icon="el-icon-delete" size="medium" @click="deleteUser(scope.row.user_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <!-- 分页 size-change 每页最大数变化 current-change 页数变化 layout 功能组件-->
      <el-pagination
          style="text-align: center; "
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 5, 9, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </div>




  </div>

  <!-- 创建新用户对话框 -->
  <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="30%"
             @close="addDialogClosed" >
    <div >
    <a-form-model   :model="form1" :rules="rules1" ref="ruleForm1"  style="text-align: center" >
            <!--姓名，账户-->
      <a-form-model-item class="Item inputbox" prop="user_name" style="width: 100%" :aria-required="true">
              <a-input
                  v-model="form1.user_name"
                  :placeholder="$t('index.name')"
                  size="large"
              >
                <a-icon
                    slot="prefix"
                    type="smile"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>
      <a-form-model-item class="Item inputbox" prop="account" style="width: 100%" >
              <a-input
                  v-model="form1.account"
                  :placeholder="$t('index.username')"
                  size="large"
              >
                <a-icon
                    slot="prefix"
                    type="user"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>


      <!--密码，确认密码-->
      <a-form-model-item class="Item inputbox" prop="password" style="width: 100%">
              <a-input
                  v-model="form1.password"
                  :placeholder="$t('index.password')"
                  size="large"
                  type="password"
              >
                <a-icon
                    slot="prefix"
                    type="key"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>

      <a-form-model-item class="Item inputbox" prop="checkPass" style="width: 100%">
              <a-input
                  v-model="form1.checkPass"
                  :placeholder="$t('index.checkPass')"
                  size="large"
                  type="password"
              >

                <a-icon
                    slot="prefix"
                    type="key"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>

      <!--电话，邮箱-->
      <a-form-model-item class="Item inputbox" prop="phone" style="width: 100%" >
              <a-input
                  v-model="form1.phone"
                  :placeholder="$t('index.phone')"
                  size="large"
                  type="phone"
              >
                <a-icon
                    slot="prefix"
                    type="phone"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>

      <a-form-model-item class="Item inputbox" prop="email" style="width: 100%" >
              <a-input
                  v-model="form1.email"
                  :placeholder="$t('index.email')"
                  size="large"
                  type="email"
              >
                <a-icon
                    slot="prefix"
                    type="mail"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>

      <a-form-model-item   prop="kind" >
        <a-radio-group v-model="form1.kind" size="large"  @change="userKindChoice"  value='云工厂' >
          <a-radio value='云工厂' >
            商家
          </a-radio>
          <a-radio value="经销商">
            用户
          </a-radio>
        </a-radio-group>
      </a-form-model-item>

      <!--工厂名称和简介-->
      <a-form-model-item class="Item inputbox" prop="f_name" style="width: 100%" >
              <a-input
                  v-model="form1.f_name"
                  :placeholder="$t('index.factory')"
                  size="large"
                  :disabled="this.ban"
              >
                <a-icon
                    slot="prefix"
                    type="home"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>


      <a-form-model-item class="Item inputbox" prop="f_brief" style="width: 100%" >
              <a-input
                  v-model="form1.f_brief"
                  :placeholder="$t('index.brief')"
                  size="large"
                  :disabled="this.ban"
              >
                <a-icon
                    type="book"
                    slot="prefix"
                    :style="{ fontSize: '16px', color: '#0088cc' }"
                />
              </a-input>
            </a-form-model-item>
    </a-form-model>
    </div>


    <!-- 内容底部区域 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser('ruleForm1')">确 定</el-button>
    </span>
  </el-dialog>

  <!-- 修改用户对话框 -->
  <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="30%" @colse="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
      <!-- 用户名 -->
      <el-form-item label="用户名" prop="account">
        <el-input v-model="editForm.account" disabled></el-input>
      </el-form-item>
      <!-- 姓名 -->
      <el-form-item label="姓名" prop="user_name">
        <el-input v-model="editForm.user_name" ></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="editForm.password"></el-input>
      </el-form-item>
      <!-- 邮箱 -->
      <el-form-item label="手机" prop="phone">
        <el-input v-model="editForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input type="email" v-model="editForm.email"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
  </el-dialog>

</div>
</template>

<script>
export default {
  name: "UserList",
  data(){
    return{
      // 请求数据
      queryInfo: {
        query: "",
            pageNum: 1,
            pageSize: 9
      },
      //新建用户
      form1:{
        account:'',
        password:'',
        user_name:'',
        phone:'',
        email:'',
        f_name:'',
        f_brief:'',
        kind:''
      },
      //修改窗口
      editForm:{
        user_id:'',
        user_name:'',
        phone:'',
        email:'',
        password:'',
      },
      //修改窗口
      editDialogVisible:false,
      editFormRules:{
        password:[
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        email:[
          { required: true, message: "请输入邮箱", trigger: "blur" },
        ],
      },
      //判断框
      rules1:{
        password:[
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
        email:[
          { required: true, message: "请输入邮箱", trigger: "blur" },
        ],
        account:[
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 1, max: 15, message: "长度再1到15位", trigger: "blur" }
        ],
        user_name:[
          { required: true, message: "请输入用户姓名", trigger: "blur" },
        ],
        phone:[
          { required: true, message: "请输入电话号码", trigger: "blur" },
        ],
        kind:[
          { required: true, message: "请选择用户种类", trigger: "blur" },
        ],
        checkPass:[
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],
      },
      addDialogVisible:false,
      ban:false,
      choice:true,
      userList:[],
          total:0,
    }

  },
  created() {
    this.getUserList();
  },
  methods:{
      async getUserList(){
        const {data:res} = await this.$http.get("allUsers",{params:this.queryInfo});
        console.log(res)
        this.userList=res.data;
        this.total=res.number;
      },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.ruleForm1.resetFields();// 重置表单项
      this.choice=true
    },
    // 添加用户
    addUser(){
      this.$refs['ruleForm1'].validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("addUser",this.form1);
        if (res != "success") {
          this.addDialogVisible = false;
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getUserList();
      })
    },
    async deleteUser(id){
        const confirmResult =await this.$confirm('用户信息将从数据库中删除，是否继续？','提示',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type:'warning'
        }).catch(err=>err)
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.delete("/deleteUser?id="+id);
      if(res!='success'){
        return this.$message.error(res)
      }else {
        this.getUserList();
        return this.$message.success("删除成功！")
      }
    }
    ,
    editUserInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("updateUser",this.editForm);
        if (res != "success") {
          return this.$message.error(res);
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getUserList();
      })
    },
    showEditDialog(user){
      this.editDialogVisible=true
      this.editForm.user_id=user.user_id
      this.editForm.account=user.account
      this.editForm.email=user.email
      this.editForm.phone=user.phone
      this.editForm.password=user.password
      this.editForm.user_name=user.user_name
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();// 重置表单项
    },
    userKindChoice(){
      if(this.form1.kind=='经销商'){
        this.ban=true
      }else{
        this.ban=false
      }
    },
    downList(){
        this.$message.info("下载开始！")
      window.location.href =  "http://localhost:9001/downUserInfo"
    }

  }
}
</script>

<style scoped>
.con{
  background-color: #FFFFFF;
  margin: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  padding-left: 10px;
  padding-right: 10px;
  padding-top: 10px;
  padding-bottom: 10px;
  overflow: hidden;
}
</style>